﻿using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using MaterialDesignColors;
using MaterialDesignThemes.Wpf;
using NPOI.POIFS.NIO;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ZhuXingFamilys.CADInformation.Entity;
using ZhuXingFamilys.CADInformation.Utils;
using ZhuXingFamilys.ExportExcel;
using ZhuXingFamilys.ModelInformation;


namespace ZhuXingFamilys.CADInformation
{
    /// <summary>
    /// CADInformationWindow.xaml 的交互逻辑
    /// </summary>
    public partial class CADInformationWindow : Window
    {
        private static CADInformationWindow staticInstance = null;
        private UIApplication uiApp;
        private List<CADInfo> CADInfos;
        private ReadCad readCad;
        private ExternalEvent readCadEvent;
        public new string Content { get; set; }



        public CADInformationWindow(UIApplication UIApp, ExternalEvent readCadEvent)
        {
            this.uiApp = UIApp;
            //ui库使用
            ColorZoneAssist.SetMode(new GroupBox(), ColorZoneMode.Light);
            Hue hue = new Hue("name", System.Windows.Media.Color.FromArgb(1, 2, 3, 4), System.Windows.Media.Color.FromArgb(1, 5, 6, 7));

            InitializeComponent();
            readCad = new ReadCad();
            // readCadEvent = ExternalEvent.Create(readCad);
            readCad.ExEvent = readCadEvent;
        }
        private void Window_Closed(object sender, EventArgs e)
        {
            staticInstance = null;
        }

        internal static CADInformationWindow GetInstance(UIApplication uiapp = null, ExternalEvent readCadEvent = null)
        {
            if (staticInstance == null && uiapp != null)
            {
                staticInstance = new CADInformationWindow(uiapp, readCadEvent);
            }

            return staticInstance;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            GeneratorCADInfoFramework();
        }




        // 生成表格框架
        private void GeneratorCADInfoFramework()
        {
            CADInfos = new List<CADInfo>()
            {
                // 信息
                new CADInfo() { classification = "信息", subitem = "", information = "图纸名称" },
                // 轴网/横坐标
                new CADInfo() { classification = "轴网", subitem = "横坐标", information = "轴线位置"},
                new CADInfo() { classification = "轴网", subitem = "横坐标", information = "轴线编号"},
                new CADInfo() { classification = "轴网", subitem = "横坐标", information = "轴线起止点" },
                new CADInfo() { classification = "轴网", subitem = "横坐标", information = "轴线总数" },
                new CADInfo() { classification = "轴网", subitem = "横坐标", information = "各轴线间距"},
                // 轴网/纵坐标
                new CADInfo() { classification = "轴网", subitem = "纵坐标", information = "轴线位置" },
                new CADInfo() { classification = "轴网", subitem = "纵坐标", information = "轴线编号" },
                new CADInfo() { classification = "轴网", subitem = "纵坐标", information = "轴线起止点" },
                new CADInfo() { classification = "轴网", subitem = "纵坐标", information = "轴线总数" },
                new CADInfo() { classification = "轴网", subitem = "纵坐标", information = "各轴线间距" },

                // 墙
                new CADInfo() { classification = "墙", subitem = "剪力墙/女儿墙", information = "名称/简称" },
                new CADInfo() { classification = "墙", subitem = "剪力墙/女儿墙", information = "强度等级" },
                new CADInfo() { classification = "墙", subitem = "剪力墙/女儿墙", information = "所在楼层" },
                new CADInfo() { classification = "墙", subitem = "剪力墙/女儿墙", information = "墙体轮廓" },
                new CADInfo() { classification = "墙", subitem = "剪力墙/女儿墙", information = "墙厚" },
                new CADInfo() { classification = "墙", subitem = "剪力墙/女儿墙", information = "起终点位置/坐标" },
                new CADInfo() { classification = "墙", subitem = "剪力墙/女儿墙", information = "材质" },

                // 柱
                new CADInfo() { classification = "柱", subitem = "结构柱", information = "名称/编号" },
                new CADInfo() { classification = "柱", subitem = "结构柱", information = "强度等级" },
                new CADInfo() { classification = "柱", subitem = "结构柱", information = "所属楼层" },
                new CADInfo() { classification = "柱", subitem = "结构柱", information = "位置" },
                new CADInfo() { classification = "柱", subitem = "结构柱", information = "截面轮廓" },
                new CADInfo() { classification = "柱", subitem = "结构柱", information = "截面轮廓点坐标" },
                new CADInfo() { classification = "柱", subitem = "结构柱", information = "截面宽度" },
                new CADInfo() { classification = "柱", subitem = "结构柱", information = "截面长度" },
                new CADInfo() { classification = "柱", subitem = "结构柱", information = "壁厚" },
                new CADInfo() { classification = "柱", subitem = "结构柱", information = "截面旋转角度" },
                new CADInfo() { classification = "柱", subitem = "结构柱", information = "截面直径" },
                new CADInfo() { classification = "柱", subitem = "结构柱", information = "材质" },
                new CADInfo() { classification = "柱", subitem = "结构柱", information = "个数统计" },

                // 板
                new CADInfo() { classification = "板", subitem = "楼板/筏板/悬挑板", information = "名称/编号" },
                new CADInfo() { classification = "板", subitem = "楼板/筏板/悬挑板", information = "强度等级" },
                new CADInfo() { classification = "板", subitem = "楼板/筏板/悬挑板", information = "所属楼层" },
                new CADInfo() { classification = "板", subitem = "楼板/筏板/悬挑板", information = "板轮廓" },
                new CADInfo() { classification = "板", subitem = "楼板/筏板/悬挑板", information = "轮廓点坐标" },
                new CADInfo() { classification = "板", subitem = "楼板/筏板/悬挑板", information = "厚度" },
                new CADInfo() { classification = "板", subitem = "楼板/筏板/悬挑板", information = "板标高/升降高度" },
                new CADInfo() { classification = "板", subitem = "楼板/筏板/悬挑板", information = "个数统计" },
                new CADInfo() { classification = "板", subitem = "楼板/筏板/悬挑板", information = "材质" },
                new CADInfo() { classification = "板", subitem = "楼板/筏板/悬挑板", information = "板洞数量" },
                new CADInfo() { classification = "板", subitem = "楼板/筏板/悬挑板", information = "板洞位置" },
                new CADInfo() { classification = "板", subitem = "楼板/筏板/悬挑板", information = "板洞轮廓坐标点" },

                // 梁
                new CADInfo() { classification = "梁", subitem = "梁", information = "名称/编号" },
                new CADInfo() { classification = "梁", subitem = "梁", information = "强度等级" },
                new CADInfo() { classification = "梁", subitem = "梁", information = "类型" },
                new CADInfo() { classification = "梁", subitem = "梁", information = "所属楼层" },
                new CADInfo() { classification = "梁", subitem = "梁", information = "轮廓" },
                new CADInfo() { classification = "梁", subitem = "梁", information = "起终点位置/坐标" },
                new CADInfo() { classification = "梁", subitem = "梁", information = "截面宽度" },
                new CADInfo() { classification = "梁", subitem = "梁", information = "截面长度" },
                new CADInfo() { classification = "梁", subitem = "梁", information = "升降高度" },
                new CADInfo() { classification = "梁", subitem = "梁", information = "所属板高度" },
                new CADInfo() { classification = "梁", subitem = "梁", information = "材质" },
                new CADInfo() { classification = "梁", subitem = "梁", information = "加腋信息" },
                new CADInfo() { classification = "梁", subitem = "梁", information = "个(跨)属统计" },

                // 屋顶/平屋顶
                new CADInfo() { classification = "屋顶", subitem = "平屋顶", information = "名称/编号" },
                new CADInfo() { classification = "屋顶", subitem = "平屋顶", information = "所属楼层" },
                new CADInfo() { classification = "屋顶", subitem = "平屋顶", information = "屋顶轮廓/轮廓点坐标" },
                new CADInfo() { classification = "屋顶", subitem = "平屋顶", information = "屋顶厚度" },
                new CADInfo() { classification = "屋顶", subitem = "平屋顶", information = "屋顶材质" },
                new CADInfo() { classification = "屋顶", subitem = "平屋顶", information = "屋顶标高" },
                // 屋顶/坡屋顶
                new CADInfo() { classification = "屋顶", subitem = "坡屋顶", information = "名称/编号" },
                new CADInfo() { classification = "屋顶", subitem = "坡屋顶", information = "所属楼层" },
                new CADInfo() { classification = "屋顶", subitem = "坡屋顶", information = "屋顶轮廓/轮廓点坐标" },
                new CADInfo() { classification = "屋顶", subitem = "坡屋顶", information = "坡度/坡向" },
                new CADInfo() { classification = "屋顶", subitem = "坡屋顶", information = "屋顶厚度" },
                new CADInfo() { classification = "屋顶", subitem = "坡屋顶", information = "屋顶材质" },
                new CADInfo() { classification = "屋顶", subitem = "坡屋顶", information = "屋顶标高" },

                 // 基础/桩
                new CADInfo() { classification = "基础", subitem = "桩", information = "名称/编号" },
                new CADInfo() { classification = "基础", subitem = "桩", information = "强度等级" },
                new CADInfo() { classification = "基础", subitem = "桩", information = "所属楼层" },
                new CADInfo() { classification = "基础", subitem = "桩", information = "位置" },
                new CADInfo() { classification = "基础", subitem = "桩", information = "升降高度(桩顶标高)" },
                new CADInfo() { classification = "基础", subitem = "桩", information = "截面轮廓/轮廓坐标点" },
                new CADInfo() { classification = "基础", subitem = "桩", information = "截面尺寸(长宽/直径)" },
                new CADInfo() { classification = "基础", subitem = "桩", information = "桩长" },
                new CADInfo() { classification = "基础", subitem = "桩", information = "砼强度等级" },
                new CADInfo() { classification = "基础", subitem = "桩", information = "材质" },

                // 基础/承台
                new CADInfo() { classification = "屋顶", subitem = "承台", information = "个数统计" },
                new CADInfo() { classification = "屋顶", subitem = "承台", information = "名称/编号" },
                new CADInfo() { classification = "屋顶", subitem = "承台", information = "强度等级" },
                new CADInfo() { classification = "屋顶", subitem = "承台", information = "所属楼层" },
                new CADInfo() { classification = "屋顶", subitem = "承台", information = "位置" },
                new CADInfo() { classification = "屋顶", subitem = "承台", information = "升降高度" },
                new CADInfo() { classification = "屋顶", subitem = "承台", information = "截面轮廓/轮廓坐标点" },
                new CADInfo() { classification = "屋顶", subitem = "承台", information = "截面尺寸(长宽/直径)" },
                new CADInfo() { classification = "屋顶", subitem = "承台", information = "砼强度等级" },
                new CADInfo() { classification = "屋顶", subitem = "承台", information = "材质" },
                new CADInfo() { classification = "屋顶", subitem = "承台", information = "个数统计" },

                // 散水
                new CADInfo() { classification = "散水", subitem = "散水/明沟", information = "名称/编号" },
                new CADInfo() { classification = "散水", subitem = "散水/明沟", information = "强度等级" },
                new CADInfo() { classification = "散水", subitem = "散水/明沟", information = "所属楼层" },
                new CADInfo() { classification = "散水", subitem = "散水/明沟", information = "轮廓" },
                new CADInfo() { classification = "散水", subitem = "散水/明沟", information = "轮廓坐标点" },
                new CADInfo() { classification = "散水", subitem = "散水/明沟", information = "厚度" },
                new CADInfo() { classification = "散水", subitem = "散水/明沟", information = "坡度" },

                // 坑/井
                new CADInfo() { classification = "坑/井", subitem = "集水坑、潜污井、电梯基坑", information = "名称/编号" },
                new CADInfo() { classification = "坑/井", subitem = "集水坑、潜污井、电梯基坑", information = "所属楼层" },
                new CADInfo() { classification = "坑/井", subitem = "集水坑、潜污井、电梯基坑", information = "位置" },
                new CADInfo() { classification = "坑/井", subitem = "集水坑、潜污井、电梯基坑", information = "轮廓" },
                new CADInfo() { classification = "坑/井", subitem = "集水坑、潜污井、电梯基坑", information = "轮廓点坐标" },
                new CADInfo() { classification = "坑/井", subitem = "集水坑、潜污井、电梯基坑", information = "升降高度" },
                new CADInfo() { classification = "坑/井", subitem = "集水坑、潜污井、电梯基坑", information = "个数统计" },

                // 后浇带
                new CADInfo() { classification = "后浇带", subitem = "后浇带", information = "名称/编号" },
                new CADInfo() { classification = "后浇带", subitem = "后浇带", information = "强度等级" },
                new CADInfo() { classification = "后浇带", subitem = "后浇带", information = "所属楼层" },
                new CADInfo() { classification = "后浇带", subitem = "后浇带", information = "位置" },
                new CADInfo() { classification = "后浇带", subitem = "后浇带", information = "轮廓" },
                new CADInfo() { classification = "后浇带", subitem = "后浇带", information = "轮廓点坐标" },
                new CADInfo() { classification = "后浇带", subitem = "后浇带", information = "厚度" },
                new CADInfo() { classification = "后浇带", subitem = "后浇带", information = "材质" },
                new CADInfo() { classification = "后浇带", subitem = "后浇带", information = "个数统计" },

                // 预留设备安装孔洞
                new CADInfo() { classification = "预留设备安装孔洞", subitem = "预留设备安装孔洞", information = "所属楼层" },
                new CADInfo() { classification = "预留设备安装孔洞", subitem = "预留设备安装孔洞", information = "孔洞位置" },
                new CADInfo() { classification = "预留设备安装孔洞", subitem = "预留设备安装孔洞", information = "截面尺寸(长宽/直径)" },
                new CADInfo() { classification = "预留设备安装孔洞", subitem = "预留设备安装孔洞", information = "孔洞轮廓坐标点" },
                new CADInfo() { classification = "预留设备安装孔洞", subitem = "预留设备安装孔洞", information = "个数统计" },



            };
            

            // this.ListView1.ItemsSource = CADInfos;

            for (int i = 0; i < CADInfos.Count(); i++)
            {
                for (int a = i + 1; a < CADInfos.Count(); a++)
                {
                    if (CADInfos[i].classification == CADInfos[a].classification)
                    {
                        
                    }
                    i++;
                }
                i++;
            }



        }








        // 识别图层
        //private void Button_Click(object sender, RoutedEventArgs e)
        //{
        //    this.WindowState = WindowState.Minimized;
        //    var doc = uiApp.ActiveUIDocument.Document;
        //    var uiSel = uiApp.ActiveUIDocument.Selection;
        //    try
        //    {
        //        var reference = uiSel.PickObject(ObjectType.PointOnElement, "点击图元上的点，按ESC退出");
        //        var element = doc.GetElement(reference);
        //        var geoObj = element.GetGeometryObjectFromReference(reference);
        //        if (geoObj.GraphicsStyleId != ElementId.InvalidElementId)
        //        {
        //            GraphicsStyle gs = doc.GetElement(geoObj.GraphicsStyleId) as GraphicsStyle;
        //            if (gs != null)
        //            {
        //                var name = gs.GraphicsStyleCategory.Name;
        //                MessageBox.Show(name);
        //            }
        //        }
        //    }
        //    catch (Autodesk.Revit.Exceptions.OperationCanceledException)
        //    {

        //    }
        //    this.WindowState = WindowState.Normal;

        //    var collector = new FilteredElementCollector(doc);
        //    var enumerator = collector.OfClass(typeof(ImportInstance)).GetEnumerator();
        //    while (enumerator.MoveNext())
        //    {
        //        ImportInstance instance = enumerator.Current as ImportInstance;
        //        CADLinkType LinkType = doc.GetElement(instance.GetTypeId()) as CADLinkType;

        //        // 判断CAD链接状态
        //        var status = LinkType.GetExternalFileReference().GetLinkedFileStatus();
        //        if(status == LinkedFileStatus.Loaded)
        //        {

        //        }
        //    }
        //}

        // 表格鼠标点击事件
        private void ListView1_MouseUp(object sender, MouseButtonEventArgs e)
        {
            CADInfo CAD = null;
            this.Hide();
            // 打开提取窗口
            LayerRecognition layer = new LayerRecognition(uiApp, CAD);

            layer.Show();

            //// 获取点击的分类
            //DataGrid datagrid = sender as DataGrid;
            //System.Windows.Point aP = e.GetPosition(datagrid);
            //IInputElement obj = datagrid.InputHitTest(aP);
            //DependencyObject target = obj as DependencyObject;
            //bool flag = true;
            //CADInfo CAD = null;
            //while (target != null)
            //{
            //    if (target is DataGridRow)
            //    {
            //        CAD = datagrid.CurrentItem as CADInfo;
            //        if (CAD != null)
            //        {
            //            var classification = CAD.classification;
            //            foreach (var item in CADInfos)
            //            {
            //                // 检查信息内容是否都为空
            //                if (item.classification == classification && !string.IsNullOrEmpty(item.content))
            //                {
            //                    flag = false;
            //                    break;
            //                }
            //            }
            //        }
            //        break;
            //    }

            //    target = VisualTreeHelper.GetParent(target);
            //}
            //if (flag && CAD != null)
            //{

            //}
        }

        // 提取信息
        public void ReadCAD(string classification, List<SelectedLayers> lineCategories, List<SelectedLayers> symbolCategories)
        {
            readCad.classification = classification;
            readCad.lineCategories = lineCategories;
            readCad.symbolCategories = symbolCategories;
            readCadEvent.Raise();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

        }

        private void ListView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        









    }
}
